package com.lzm.ydpt.module.mall.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.byt.framlib.base.BaseActivity;
import com.byt.framlib.baseadapter.lv.LvCommonAdapter;
import com.byt.framlib.baseadapter.lv.LvViewHolder;
import com.byt.framlib.basemvp.BasePresenter;
import com.byt.framlib.commonutils.rx.RxBus;
import com.byt.framlib.commonwidget.NoScrollGridView;
import com.byt.framlib.commonwidget.NormalTitleBar;
import com.byt.framlib.commonwidget.OnNoDoubleClickListener;
import com.byt.framlib.commonwidget.onclicktwo.AntiShake;
import com.lzm.ydpt.Constants;
import com.lzm.ydpt.R;
import com.lzm.ydpt.entity.mall.MallOrderBean;
import com.lzm.ydpt.entity.mall.ProductBean;
import com.lzm.ydpt.entity.mall.ProductCartBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 订单支付结果
 */
public class OrderPayStatusActivity extends BaseActivity {

    @BindView(R.id.ntb_order_pay_status)
    NormalTitleBar ntb_order_pay_status;

    @BindView(R.id.img_order_state)
    ImageView img_order_state;
    @BindView(R.id.tv_order_state)
    TextView tv_order_state;
    @BindView(R.id.tv_show_fail)
    TextView tv_show_fail;

    @BindView(R.id.ll_pay_status_use)
    LinearLayout ll_pay_status_use;
    @BindView(R.id.tv_see_shop_home)
    TextView tv_see_shop_home;
    @BindView(R.id.tv_see_order_details)
    TextView tv_see_order_details;

    @BindView(R.id.ll_guess_like_layout)
    LinearLayout ll_guess_like_layout;
    @BindView(R.id.gv_guess_like)
    NoScrollGridView gv_guess_like;

    private MallOrderBean orderBean;
    /**
     * 0 成功
     * 1 失败
     */
    private int order_status = 0;
    /**
     *
     */
    private int order_jump_type = 0;
    private List<ProductBean> mProductList = new ArrayList<>();
    private LvCommonAdapter<ProductBean> productAdapter = null;

    @Override
    public int getLayoutId() {
        return R.layout.activity_mall_order_pay_status;
    }

    @Override
    public BasePresenter initPreData() {
        return null;
    }

    @Override
    public void initView() {
        initTitle();
        orderBean = getIntent().getParcelableExtra(Constants.INP_ORDER_BEAN);
        order_status = getIntent().getIntExtra(Constants.INP_ORDER_STATUS, 0);
        order_jump_type = getIntent().getIntExtra(Constants.INP_ORDER_JUMP_TYPE, 1);
        if (order_status == 1) {
            RxBus.getDefault().postSticky(new ProductCartBus()); // 刷新订单数字,带发货加一
            tv_order_state.setText("支付成功");
            img_order_state.setSelected(true);
            tv_show_fail.setVisibility(View.GONE);
            ll_pay_status_use.setVisibility(View.VISIBLE);
            ll_guess_like_layout.setVisibility(View.VISIBLE);
            initAdapter();
            getProductList();
        } else if (order_status == 0) {
            tv_order_state.setText("支付失败");
            img_order_state.setSelected(false);
            tv_show_fail.setVisibility(View.VISIBLE);
            ll_pay_status_use.setVisibility(View.GONE);
            ll_guess_like_layout.setVisibility(View.GONE);
        }
    }

    private void initAdapter() {
        productAdapter = new LvCommonAdapter<ProductBean>(this, mProductList, R.layout.item_product_grid_layout) {
            @Override
            protected void convert(LvViewHolder viewHolder, ProductBean productBean, int position) {

            }
        };
        gv_guess_like.setAdapter(productAdapter);
    }

    private void initTitle() {
        ntb_order_pay_status.setOnBackListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                finish();
            }
        });
    }

    private void getProductList() {

    }

    @OnClick({R.id.tv_see_shop_home, R.id.tv_see_order_details, R.id.tv_show_fail})
    public void OnClick(View v) {
        if (AntiShake.check(v.getId())) {
            return;
        }
        switch (v.getId()) {
            case R.id.tv_see_shop_home:
                Bundle mainBundle = new Bundle();
//                mainBundle.putInt(MallMainActivity.INP_MAIN_POSITION, 1);
                openActivity(MallMainActivity.class, mainBundle);
                finish();
                break;
            case R.id.tv_see_order_details:
            case R.id.tv_show_fail:
                if (order_jump_type == 1) {
                    Bundle bundle = new Bundle();
                    bundle.putInt(MallOrderListActivity.INP_ORDER_JUMPMODE, 1);
                    openActivity(MallOrderListActivity.class, bundle);
                } else {
                    Bundle detailsBundle = new Bundle();
                    detailsBundle.putLong(Constants.INP_ORDER_ID, orderBean.getOrder_id());
                    openActivity(MallOrderDetailsActivity.class, detailsBundle);
                }
                finish();
                break;
        }
    }
}